package com.zhangyu.shadow.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.view.View;

import com.zhangyu.shadow.core.delegate.ShadowDelegate;
import com.zhangyu.shadow.core.net.RestClient;
import com.zhangyu.shadow.core.net.callback.IError;
import com.zhangyu.shadow.core.net.callback.IFailure;
import com.zhangyu.shadow.core.net.callback.ISuccess;
import com.zhangyu.shadow.core.util.regex.RegexUtils;
import com.zhangyu.shadow.ec.R;
import com.zhangyu.shadow.ec.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author zhangyu
 * @date 2017/10/25
 */

public class SignUpDelegate extends ShadowDelegate {
    private static final String TAG = "SignUpDelegate";
    @BindView(R2.id.til_name)
    TextInputEditText mEtName;
    @BindView(R2.id.til_email)
    TextInputEditText mEtEmail;
    @BindView(R2.id.til_phone)
    TextInputEditText mEtPhone;
    @BindView(R2.id.til_password)
    TextInputEditText mEtPassword;
    @BindView(R2.id.til_re_password)
    TextInputEditText mEtRePassword;

    private ISignListener mISignListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof ISignListener){
            mISignListener = (ISignListener) activity;
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }

    @OnClick(R2.id.btn_sign_up)
    public void onSignUp() {
        if (checkForm()) {
            RestClient.builder().url("RestServer/api/user_profile.php")
                    .loader(getContext())
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            SignHandler.onSignIn(response,mISignListener);
                        }
                    })
                    .failure(new IFailure() {
                        @Override
                        public void onFailure() {

                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {

                        }
                    })
                    .build()
                    .post();
        }
    }

    @OnClick(R2.id.tv_login)
    public void onLogin() {
        start(new SignInDelegate(), SINGLETASK);
    }

    private boolean checkForm() {
        String name = mEtName.getText().toString();
        String email = mEtEmail.getText().toString();
        String phone = mEtPhone.getText().toString();
        String password = mEtPassword.getText().toString();
        String rePassword = mEtRePassword.getText().toString();

        boolean isPass = true;
        if (!RegexUtils.isUsername(name)) {
            mEtName.setError("请输入姓名");
            isPass = false;
        } else {
            mEtName.setError(null);
        }

        if (!RegexUtils.isEmail(email)) {
            mEtEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEtEmail.setError(null);
        }

        if (!RegexUtils.isMobileExact(phone)) {
            mEtPhone.setError("错误的手机号码");
            isPass = false;
        } else {
            mEtPhone.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            mEtPassword.setError("密码不能小于6位");
            isPass = false;
        } else {
            mEtPassword.setError(null);
        }

        if (rePassword.isEmpty() || !rePassword.equals(password)) {
            mEtRePassword.setError("两次密码不相同");
            isPass = false;
        } else {
            mEtRePassword.setError(null);
        }
        return isPass;
    }

}
